package alibaba;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Utils {

	public static String calculateSigStr(File file) {

		byte[] hashFile = calculateSig(file);
		if (hashFile == null)
			return zeroSig();
		
		try {
			
			return Base64.encodeBytes(hashFile, Base64.URL_SAFE);
		} catch (IOException e) {
			return zeroSig();
		}
	}

	public static byte[] calculateSig(File file) {
		InputStream is = null;
		byte[] sha1hash = null;
		try {
			is = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			return null;
		}

		// Get the size of the file
		long length = file.length();

		// You cannot create an array using a long type.
		// It needs to be an int type.
		// Before converting to an int type, check
		// to ensure that file is not larger than Integer.MAX_VALUE.
		if (length > Integer.MAX_VALUE) {
			// File is too large
		}

		// Create the byte array to hold the data
		byte[] bytes = new byte[(int)length];

		// Read in the bytes
		int offset = 0;
		int numRead = 0;
		try {
			while (offset < bytes.length
					&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
				offset += numRead;
			}


			// Ensure all the bytes have been read in
			if (offset < bytes.length) {
				System.out.println("Could not completely read file " + file.getName());
				return null;
			}

			// Close the input stream and return bytes
			is.close();

			MessageDigest md = null;
			try {
				md = MessageDigest.getInstance("SHA-1");
			} catch (NoSuchAlgorithmException e) {}
			md.update(bytes);
			sha1hash = md.digest();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sha1hash;
	}

	public static void storeListing(Listing listing, File listingFile) {
		try {
			if (!listingFile.exists())
				listingFile.createNewFile();
			XMLEncoder e = new XMLEncoder(
					new BufferedOutputStream(
							new FileOutputStream(listingFile.getAbsolutePath())));
			e.writeObject(listing);
			e.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void removeListing(Listing listing, File listingFile) {
		try {
			if (!listingFile.exists())
				listingFile.createNewFile();
			XMLEncoder e = new XMLEncoder(
					new BufferedOutputStream(
							new FileOutputStream(listingFile.getAbsolutePath())));
			e.remove(listing);
			e.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Listing loadListing(File listingFile) {
		Object listingObj = null;
		try {
			//listingFile.createNewFile();
			XMLDecoder e = new XMLDecoder(
					new BufferedInputStream(
							new FileInputStream(listingFile.getAbsolutePath())));
			listingObj = e.readObject();
			e.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Listing listing = (Listing) listingObj;
		return listing;
	}

	public static String extractVirtualDirName(String fileName) {

		int idx = fileName.substring(0, fileName.length() - 1).lastIndexOf('/');
		fileName = fileName.substring(0, idx + 1);
		return fileName;
	}
	
	public static String extractLocalDirName(String fileName) {

		int idx = fileName.substring(0, fileName.length() - 1).lastIndexOf(File.separatorChar);
		fileName = fileName.substring(0, idx + 1);
		return fileName;
	}

	public static int getFileNameInt(String localName) {
		int idx = localName.lastIndexOf(File.separatorChar);
		String fileNumStr = localName.substring(idx + 1, localName.length());
		int num = new Integer(fileNumStr);
		return num;
	}

	public static String extractFileName(String fileName) {
		int idx = fileName.substring(0, fileName.length() - 1).lastIndexOf('/');
		fileName = fileName.substring(idx + 1);
		return fileName;
	}

	public static String getLocalFileSeparator() {
		if (File.separatorChar == '/')
			return File.separator;
		else
			return File.separator + File.separator;
	}
	
	public static String zeroSig() { 
		return "0000000000000000000000000000";
	}
}
